package com.sy.service.impl;

import com.alibaba.fastjson.JSON;
import com.sy.dto.DriClassDto;
import com.sy.dto.DriSchoolDto;
import com.sy.dto.DriSchools;
import com.sy.dto.Params;
import com.sy.entity.*;
import com.sy.service.DriSchoolService;
import com.sy.utils.ParamsUtils;
import com.sy.utils.RedisOperations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


@Service
public class DriSchoolServiceImpl extends BaseServiceImpl implements DriSchoolService {
    @Autowired
    private RedisOperations redisOperations;

    /**
     * 通过条件查询驾校信息
     *
     * @return
     * @params params
     */
    @Override
    public List<DriSchools> getDriSchoolListByParams(Params params) {
        List<DriSchools> driSchoolsList;
        String key = "driSchoolListCount" + ParamsUtils.add(params);
        if (redisOperations.exists(key)) {
            System.out.println("从Redis中直接获取...");
            driSchoolsList = JSON.parseArray(redisOperations.get(key), DriSchools.class);
        } else {

            PriceRegion priceRegion = new PriceRegion();
            String sort = "";
            System.out.println(params.toString());
            //获取价格区间
            if (!Objects.isNull(params.getPrice())) {
                priceRegion = priceRegionMapper.getPriceRegionById(params.getPrice());
            }
            //获取排序方式
            if (!Objects.isNull(params.getSortType())) {
                System.out.println(String.valueOf(params.getSortType()).trim());
                sort = (params.getSortType() == 1) ? "" : (params.getSortType() == 2 ? "price" : "count");
                System.out.println(sort);
            }
            //分页
            Integer start = (params.getPage() - 1) * 10;
            //通过城市id获得该城市及区域内的所有区域集合
            List<Province> provinces = new ArrayList<>();
            if (!Objects.isNull(params.getPid())) {
                Province provinceByPid = provinceMapper.getProvinceByPid(params.getPid());
                provinces.add(provinceByPid);
                if (params.getPid() < 32) {
                    provinceMapper.getProvincesByProvinceId(params.getPid()).forEach(province -> provinces.add(province));
                }
            }
            //通过城市区域集合获取其id的集合
            List<Integer> pIds = provinces.stream().map(province -> province.getPid()).collect(Collectors.toList());
            //通过条件查询驾校列表
            List<DriSchool> driSchoolList = driSchoolMapper.getDriSchoolByParams(pIds ,params.getKeyWord(), sort, params.getDriverType(), priceRegion.getStartPrice(), priceRegion.getEndPrice(), start);
            //获取详细驾校信息集合
            driSchoolsList = driSchoolList.stream().map(driSchool -> {
                DriSchools driSchools = new DriSchools();
                driSchools.setDriSchool(driSchool);
                List<DriClass> driClasses = driClassMapper.getDriClassByDriSchoolId(driSchool.getDriSchoolId());
                driSchools.setDriClass(driClasses);
                return driSchools;
            }).collect(Collectors.toList());
            //存入redis中
            redisOperations.set(key, JSON.toJSONString(driSchoolsList));
        }
        return driSchoolsList;

    }

    /**
     * 通过驾校id查询驾校详细信息
     *
     * @param id
     * @return
     */

    @Override
    public DriSchoolDto getDriSchoolById(Integer id) {
//        String key = "driSchoolDto" + id;
        DriSchoolDto driSchoolDto;
//        if (redisOperations.exists(key)) {
//            System.out.println("从Redis中直接获取...");
//            driSchoolDto = JSON.parseObject(redisOperations.get(key), DriSchoolDto.class);
//        } else {
            //通过驾校id获取驾校信息
            DriSchool driSchool = driSchoolMapper.getDriSchoolById(id);
            //通过驾校id获取驾校存在班级的集合
            List<DriClass> driClass = driClassMapper.getDriClassByDriSchoolId(id);
            //通过班级信息集合获取其班级详细信息集合
            List<DriClassDto> driClasses = driClass.stream().map(driClass1 -> {
                DriClassDto driClassDto = new DriClassDto();
                List<CarDistribution> carDistributionList = carDistributionMapper.getCarDistributionListByCid(driClass1.getId());
                driClassDto.setCarDistributionList(carDistributionList);
                driClassDto.setDriClass(driClass1);
                return driClassDto;
            }).collect(Collectors.toList());
            //通过驾校id获取该驾校的所有评论
            List<Commit> commitList = commitMapper.getCommitListByDrId(id,1);
            //通过驾校id获取驾校服务内容集合
            List<Serve> serveList = serveMapper.getServeListByDriSchoolId(id);
            driSchoolDto = new DriSchoolDto(driSchool, serveList, driClasses, commitList);
//            redisOperations.set(key, JSON.toJSONString(driSchoolDto));
//        }
        return driSchoolDto;
    }

    /**
     * 通过条件获取驾校的总页码
     */
    @Override
    public Integer getCountByParams(Params params) {
        String key = "driSchoolListCount" + ParamsUtils.add(params);
        Integer count;
//        if (redisOperations.exists(key)) {
//            System.out.println("从Redis中直接获取...");
//            System.out.println(redisOperations.get(key));
//            count =JSON.parseObject(redisOperations.get(key), Integer.class) ;
//        } else {
            PriceRegion priceRegion = new PriceRegion();
            String sort = "";
            System.out.println(params.toString());
            //获取价格区间
            if (!Objects.isNull(params.getPrice())) {
                priceRegion = priceRegionMapper.getPriceRegionById(params.getPrice());
            }
            //获取排序方式
            if (!Objects.isNull(params.getSortType())) {
                System.out.println(String.valueOf(params.getSortType()).trim());
                sort = (params.getSortType() == 1) ? "" : (params.getSortType() == 2 ? "price" : "count");
                System.out.println(sort);
            }
            List<Province> provinces = new ArrayList<>();
            //通过城市id获得该城市及区域内的所有区域集合
            if (!Objects.isNull(params.getPid())) {
                Province provinceByPid = provinceMapper.getProvinceByPid(params.getPid());
                provinces.add(provinceByPid);
                if (params.getPid() < 32) {
                    provinceMapper.getProvincesByProvinceId(params.getPid()).forEach(province -> provinces.add(province));
                }
            }
            //通过城市id获得该城市及区域内的所有区域集合
            List<Integer> pIds = provinces.stream().map(province -> province.getPid()).collect(Collectors.toList());
            //通过条件查询驾校列表的总数量
            Integer count1 = (int) driSchoolMapper.getCountByParams(pIds ,params.getKeyWord(), sort, params.getDriverType(), priceRegion.getStartPrice(), priceRegion.getEndPrice());
            count = (count1 / 10);
            if(10*count<count1 & count>0){
                count=count+1;
//            }
//            redisOperations.set(key, JSON.toJSONString(count));
        }
        return count;
    }
}
